package app.repositories.impl;

import javax.persistence.EntityManager;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import app.models.CH;
import app.repositories.CHRepository;
import app.repositories.Repository;
import br.com.caelum.vraptor.ioc.Component;

@Component
public class CHRepositoryImpl extends Repository<CH, Long> implements CHRepository {

	private final String ID_CH = "id";
	
	public CHRepositoryImpl(EntityManager entityManager) {
		super(entityManager);
	}
	
	public CH findLastCH() {
		Criteria criteria = createCriteria();
		
		criteria.addOrder(Order.asc(ID_CH));
		criteria.setMaxResults(1);
		
		return (CH) criteria.uniqueResult();
	}
	
	public CH findByConvenioId(Integer convenioId) {
		Criteria criteria = createCriteria();
		
		criteria.add(Restrictions.eq("idConvenio", convenioId));
		
		return (CH) criteria.uniqueResult();
	}

}